/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package bllImplement;

import bllInterface.Account;
import bllInterface.BLLQuerry;
import bllobject.AccountImpl;
import dalfactory.SDALFactory;
import java.rmi.RemoteException;
import java.util.ArrayList;
import utils.DataRow;
import utils.DataTable;

/**
 *
 * @author Mrkupi
 */
public class BLLQuerryImpl implements BLLQuerry
{
    private static BLLQuerryImpl inst;
    
    private BLLQuerryImpl()
    {
    }
    
    public static BLLQuerry getInstance()
    {
        if( inst    ==  null )
        {
            inst    =   new BLLQuerryImpl();
        }
        return inst;
    }
    
    @Override
    public ArrayList<Account> getUsers( String name ) throws RemoteException 
    {
        DataTable dtAcc     =   SDALFactory.getInstance().getQuerry().getAccountByName( name );
        DataTable dtAccType =   SDALFactory.getInstance().getQuerry().getAllAccountType();
        ArrayList<Account> users  =   new ArrayList();
        
        if( dtAcc.size() > 0 && dtAccType.size() > 0 )
        {
            for( int i = 0; i < dtAcc.size(); ++i)
            {
                String description = "";
                boolean isFind = false;
                DataRow row = dtAcc.get(i);
                for( int j = 0; !isFind && j < dtAccType.size(); ++j)
                {
                    DataRow rowType = dtAccType.get( j );
                    if( rowType.get( "TypeID" ) == null ? row.get( "TypeID" ) == null : rowType.get( "TypeID" ).equals(row.get( "TypeID" )) )
                    {
                        description =   rowType.get("Description");
                        isFind      =   true;
                    }
                }
                if( dtAcc.size() > 0 && !("".equals(description)) )
                {
                    users.add(AccountImpl.createRemoteAccount(row.get("UserID"), row.get("UserName"), Integer.parseInt(row.get("Level")),
                                                                Integer.parseInt(row.get("Experience")), description) );
                }
            }
        }
        
        return users;
    }
}
